#ifndef APPCONFIG_H
#define APPCONFIG_H

#include "BasicType.h"
#include <stddef.h>
#include <string>
#include <sstream>
#include <cstring>
#include <QString>


class AppConfig
{
public:
    AppConfig();

	static QString getFilePath();

	static void setFilePath(const std::string& filepath);

    static bool get(const QString& key, QString& value);

    static bool set(const QString& key, const QString& value);



    template < class T >
    static bool getValue(const QString& key, T& t)
    {
        QString value;
        if (!get(key, value))
        {
            return false;
        }

        std::string str = value.toStdString();
        std::istringstream iss(str);
        iss >> t;
        return true;
    }

    
    static bool getValue(const QString& key, bool& t)
    {
        QString value;
        if (!get(key, value))
        {
            return false;
        }

        t = true;
        if (value.isEmpty())
        {
            t = false;
        }
        else if (value == "false")
        {
            t = false;
        }
        else if (value == "0")
        {
            t = false;
        }
        return true;
    }


    template < class T >
    static bool setValue(const QString& key, const T& t)
    {
        std::ostringstream oss;
        oss << t;
        QString qstr = QString::fromStdString(oss.str());
        return set(key, qstr);
    }

    static QString getString(const QString& key, const QString& defValue=QString())
    {
        QString value(defValue);
        get(key, value);
        return value;
    }

    static  int getInt(const QString& key, int defValue=0)
    {
        int value = defValue;
        getValue(key, value);
        return value;
    }

    static  bool getBool(const QString& key, bool defValue=false)
    {
        bool value = defValue;
        getValue(key, value);
        return value;
    }

    static  double getDouble(const QString& key, double defValue=0)
    {
        double value = defValue;
        getValue(key, value);
        return value;
    }

    static  int64_t getInt64(const QString& key, int64_t defValue=0)
    {
        int64_t value = defValue;
        getValue(key, value);
        return value;
    }


    static  bool setInt(const QString& key, int value)
    {
        return setValue(key, value);
    }

    static  bool setBool(const QString& key, bool value)
    {
        return setValue(key, value);
    }

    static  bool setDouble(const QString& key, double value)
    {
        return setValue(key, value);
    }

    static  bool setInt64(const QString& key, int64_t value)
    {
        return setValue(key, value);
    }


protected:
	static QString	s_filepath;

};

#endif // APPCONFIG_H
